package net.example.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import net.example.activity.adapter.SearchFriendAdapter;
import net.example.manager.ClientGlobalCache;
import net.example.manager.Manager;
import net.example.model.GGGroup;
import net.example.model.GGUser;
import net.example.model.UserBasicModel;
import net.example.ggtalk.R;
import net.example.utils.CommonHelper;
import net.example.utils.IntentUtils;

import java.util.ArrayList;
import java.util.List;

public class SearchFriendActivity extends Activity implements TextView.OnEditorActionListener, AdapterView.OnItemClickListener {
    private EditText input;
    private ListView listView;
    private TextView search_resultStr;
    private List<UserBasicModel> userList=new ArrayList<UserBasicModel>() ;
    private SearchFriendAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_friend);
        this.initView();
    }

    private void initView()
    {
        TextView pageTitle=(TextView)findViewById(R.id.headerText);
        pageTitle.setText("查找用户和讨论组");
        search_resultStr =(TextView) findViewById(R.id.search_resultStr);
        input=(EditText)findViewById(R.id.et_input);
        input.setOnEditorActionListener(this);
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String id=s.toString();
                List<GGUser> GGUsers = ClientGlobalCache.getInstance().searchUserFuzzy(id);
                List<GGGroup> GGGroups = ClientGlobalCache.getInstance().searchGroupFuzzy(id);
                search_resultStr.setText("无本地搜索结果，点击搜索按钮从服务端获取结果");
                setSearchResult(GGUsers, GGGroups);
            }
        });
        listView=(ListView)findViewById(R.id.listview_user);
        listView.setOnItemClickListener(this);
        adapter = new SearchFriendAdapter(this,this.userList,R.layout.friend_child);
//        adapter.setOpenCacheView(true);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId== EditorInfo.IME_ACTION_SEARCH)
        {
            String idOrName=v.getText().toString();
            List<GGUser> temp_users=Manager.getInstance().doSearchUser(idOrName);
            List<GGGroup> temp_groups=Manager.getInstance().searchGroupList(idOrName);
            setSearchResult(temp_users,temp_groups);
            return false;
        }
        else return actionId == EditorInfo.IME_ACTION_NONE;
    }



    private void setSearchResult(List<GGUser> GGUsers, List<GGGroup> GGGroups)
    {
        List<UserBasicModel> temp_users=new ArrayList<>();
        temp_users.addAll(CommonHelper.ggUserListConvertToUserBasicModel(GGUsers));
        temp_users.addAll(CommonHelper.ggGroupListConvertToUserBasicModel(GGGroups));
        this.userList.clear();
        if(temp_users==null||temp_users.size()==0)
        {
            search_resultStr.setText("没有找到符合搜索条件的结果");
            search_resultStr.setVisibility(View.VISIBLE);
        }
        else
        {
            this.userList.addAll(temp_users);
            search_resultStr.setVisibility(View.GONE);
        }
        this.adapter.notifyDataSetChanged();
    }

    public void finish(View view)
    {
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        UserBasicModel model=this.userList.get(position);
        if(model.getIsGroup())
        {
            IntentUtils.startActivityForString(SearchFriendActivity.this, GroupDetailActivity.class, "requestID", model.getUserID());
        }else {
            Manager.getInstance().moveToUserDetail(this,model.getUserID());
        }
    }
}
